import { Component, OnInit, ViewChild, ViewContainerRef, ComponentRef, ComponentFactoryResolver, EventEmitter } from '@angular/core';
import { AddService } from './add.service'
import { DataService } from '../../../../../services/data.service';
import { ApplyFundGroupComponent } from '../apply-fund-group/apply-fund-group.component';
import { NapplyFundGroupComponent } from '../napply-fund-group/napply-fund-group.component';
import { Router, ActivatedRoute } from '@angular/router';
import * as _ from 'lodash';

@Component({
  selector: 'app-add',
  templateUrl: './add.component.html',
  styleUrls: ['./add.component.scss'],
  providers: [AddService]
})
export class AddComponent implements OnInit {
  //查看#applyFundGroup modal7
  @ViewChild("applyFundGroup", { read: ViewContainerRef })
  applyFundGroup: ViewContainerRef;
  applyFundGroupComponent: ComponentRef<ApplyFundGroupComponent>;
  //查看#napplyFundGroup modal8
  @ViewChild("napplyFundGroup", { read: ViewContainerRef })
  napplyFundGroup: ViewContainerRef;
  napplyFundGroupComponent: ComponentRef<NapplyFundGroupComponent>;

  public sType: any;
  // 添加请求体

  // 用于判断选中项是否改变
  public differenceItem: any = []

  public addBody: any = {
    sFgName: '',
    sFgDesc: '',
    sMemberTemp: '',
    sMemberFundnos: ''
  }
  //重置请求体
  public resetAddBody: any = {
    sFgName: '',
    sFgDesc: '',
    sMemberTemp: '',
    sMemberFundnos: ''
  }

  constructor(
    public resolver: ComponentFactoryResolver,
    public router: Router,
    private route: ActivatedRoute,
    public addService: AddService,
    public dataService: DataService
  ) { }

  ngOnInit() {

    this.dataService.nQDIIsFgMemberValidEmitter = new EventEmitter();
    this.dataService.nQDIIsFgMemberValidEmitter.subscribe((value?: any) => {
      // if (value) {
      console.log(value);
      if (value == 'no data') {
    
        window['swal']("提示", "没有选择数据!", "warning");
        this.addBody.sMemberTemp = '';
        window['$']('#applyFundGroup').modal('hide');
        window['$']('#napplyFundGroup').modal('hide');

      }
      else if (value !== 'no data' && value != '') {
       
        if(this.addBody.sMemberTemp==null){
     
          this.addBody.sMemberTemp = value;
          window['$']('#applyFundGroup').modal('hide');
          window['$']('#napplyFundGroup').modal('hide');
        }
        else{
     
           // 如果非空，转成数组,用于判断值是否修改。
        let valueArray = value.split(',')
        let oldData = this.addBody.sMemberTemp.split(',');

        oldData =  this.addBody.sMemberTemp.split(',');
        let differenceItem = oldData
        .filter(x => valueArray.indexOf(x) == -1)
        .concat(valueArray.filter(x => oldData.indexOf(x) == -1));

        // let differenceItem = _.difference(valueArray, oldData);
    
        if (differenceItem.length == 0) {

          window['$']('#applyFundGroup').modal('hide');
          window['$']('#napplyFundGroup').modal('hide');
          window['swal']("提示", "没有选择数据!", "warning");
        } else {
      
          this.addBody.sMemberTemp = value;
  
          window['$']('#applyFundGroup').modal('hide');
          window['$']('#napplyFundGroup').modal('hide');
        }
        }
       
      }

      // } 
    });
    this.dataService.nQDIIsMemberFundnosEmitter = new EventEmitter();
    this.dataService.nQDIIsMemberFundnosEmitter.subscribe((value?: any) => {
      if (value == 'no data') {
      
        this.addBody.sMemberFundnos =''
      }

      else if (value !== 'no data' && value != '') {
        
         this.addBody.sMemberFundnos
        let valueArray = value.split(',')
        let oldData =  this.addBody.sMemberTemp.split(',');
        let differenceItem = _.difference(valueArray, oldData);
        if (differenceItem.length == 0) {
          this.addBody.sMemberFundnos = value;
        
        }
        else {
          this.addBody.sMemberFundnos = value;
       
        }
        
      }
     
     
      // if (value) {
      //   this.addBody.sMemberFundnos = value;
      //   console.log(this.addBody.sMemberFundnos);
      // }
    });
  }

  //重置请求体
  resetAddbody() {
    this.addBody.sFgName = this.resetAddBody.sFgName;
    this.addBody.sFgDesc = this.resetAddBody.sFgDesc;
    this.addBody.sMemberTemp = this.resetAddBody.sMemberTemp;
    this.addBody.sMemberFundnos = this.resetAddBody.sMemberFundnos;
  }

  // 新增产品
  nqdiigmAddBasicInfo() {
    let that = this;
    if (this.requireAdd()) {
      window["swal"]({
        title: "提示",
        text: "是否确定保存基础信息?",
        type: "warning",
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        showCancelButton: true,
        closeOnConfirm: false,
        closeOnCancel: true,
        showLoaderOnConfirm: true
      }, function (isConfirm) {
        if (isConfirm) {
          that.addService.addProduct(that.sType, that.addBody).subscribe(data => {
            if (data) {
              that.dataService.rcmiEditEmitter.emit('add');
              window['swal']("成功", "请等待添加待审!", "success");
            }
          });
        }
      })
    }

  }
  // 新增必填校验
  requireAdd() {
    let that = this;
    let requiredAddTip = [];
    let sFgName = _.trim(this.addBody.sFgName);
    if (sFgName == "" || sFgName == null) {
      requiredAddTip.push("群组名称");
    }
    if (requiredAddTip.length > 0) {
      // let requiredAddTips = requiredAddTip.join("、");
      window['swal']("提示", "请填写:" + requiredAddTip[0], "warning");
      return false;
    } else {
      return true;
    }
  }

  //显示#showApplyFundGroup modal7
  showApplyFundGroupModal() {
    console.log(this.sType);
    if (this.sType == "1") {
      this.showApplyFundGroup();
      window['$']('#applyFundGroup').modal('show');
    } else if (this.sType == "0") {
      this.showNapplyFundGroup();
      window['$']('#napplyFundGroup').modal('show');
    }
  }

  //查看#showApplyFundGroup modal7创建
  showApplyFundGroup() {
    // 判断组件是否已创建，如果创建先注销
    if (this.applyFundGroupComponent && this.applyFundGroupComponent.instance) {
      this.applyFundGroupComponent.destroy();
    }
    // 动态创建组件的实例
    const applyFundGroupComponent = this.resolver.resolveComponentFactory(ApplyFundGroupComponent);
    this.applyFundGroupComponent = this.applyFundGroup.createComponent(applyFundGroupComponent);
    if (this.addBody.sMemberFundnos != '' && this.addBody.sMemberFundnos != null && this.addBody.sMemberFundnos != undefined) {
      this.applyFundGroupComponent.instance.checkedIdList = this.addBody.sMemberFundnos;
    }

  }

  //查看#showNapplyFundGroup modal8创建
  showNapplyFundGroup() {
    // 判断组件是否已创建，如果创建先注销
    if (this.napplyFundGroupComponent && this.napplyFundGroupComponent.instance) {
      this.napplyFundGroupComponent.destroy();
    }
    // 动态创建组件的实例
    const napplyFundGroupComponent = this.resolver.resolveComponentFactory(NapplyFundGroupComponent);
    this.napplyFundGroupComponent = this.napplyFundGroup.createComponent(napplyFundGroupComponent);
    if (this.addBody.sMemberFundnos != '' && this.addBody.sMemberFundnos != null && this.addBody.sMemberFundnos != undefined) {
      this.napplyFundGroupComponent.instance.checkedIdList = this.addBody.sMemberFundnos;
    }
  }
}
